package com.huyanqi.front.rest.service.impl;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.huyanqi.core.RetdCodeType;
import com.huyanqi.core.RetdObj;
import com.huyanqi.core.service.impl.BaseFrontServiceImpl;
import com.huyanqi.dao.PraiseDAO;
import com.huyanqi.entity.Praise;
import com.huyanqi.front.rest.service.PraiseRestService;

@Service("PraiseRestService")
public class PraiseRestImpl extends BaseFrontServiceImpl<Praise,Long, PraiseDAO> implements PraiseRestService{

	@Override
	@Resource(name="PraiseDAO")
	public void setEntityDao(PraiseDAO entityDao) {
		this.entityDao=entityDao;
	}

	@Override
	public RetdObj<Praise> toPraise(Long uid, Integer type, Long id) {
		RetdObj<Praise> retd = new RetdObj<Praise>();
		Praise p = new Praise();
		p.setPUid(uid);
		if(type==1){
			//赞店铺
			if(entityDao.executeQuery("FROM Praise WHERE pUid = ? AND pShopid = ?", new Object[]{uid,id}).size()>0){
				//已赞过 退出
				retd.setCode(RetdCodeType.EX_AUTH);
				return null;
			}
			p.setPShopid(id);
		}else if(type==2){
			//赞分享
			if(entityDao.executeQuery("FROM Praise WHERE pUid = ? AND pShareid = ?", new Object[]{uid,id}).size()>0){
				//已赞过 退出
				retd.setCode(RetdCodeType.EX_AUTH);
				return null;
			}
			p.setPShareid(id);
		}
		try {
			retd.setRealObj(entityDao.saveEntity(p));
			retd.setCode(RetdCodeType.PASS_OK);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return retd;
	}

}
